/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaPesquisarSaida.java
 *
 * Created on 10/12/2012, 16:35:27
 */
package gui;

import Fachada.Fachada;
import classes.ItensProduto;
import classes.Produto;
import classes.Saida;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.DaoException;
import erro.GeralException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author George
 */
public class telaPesquisarSaida extends javax.swing.JFrame {
    private static Fachada f;
    /** Creates new form telaPesquisarSaida */
    public telaPesquisarSaida() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        txtBoxDataSaida = new javax.swing.JFormattedTextField();
        ButtonPesqSaida = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TableSaida = new javax.swing.JTable();
        ButtonDeletarSaida = new javax.swing.JButton();
        ButtonListarSaida = new javax.swing.JButton();
        ButtonCancelar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        LabelCodSaida = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        LabelNumLote = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        LabelProduto = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("Pesquisar Saida");

        jLabel2.setText("Data Saida:");

        try {
            txtBoxDataSaida.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        ButtonPesqSaida.setText("PESQUISAR");
        ButtonPesqSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonPesqSaidaActionPerformed(evt);
            }
        });

        TableSaida.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Cod. Saida", "Produto", "Num. Lote", "Quant. Itens", "Data Saida"
            }
        ));
        TableSaida.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableSaidaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TableSaida);

        ButtonDeletarSaida.setText("DELETAR");
        ButtonDeletarSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDeletarSaidaActionPerformed(evt);
            }
        });

        ButtonListarSaida.setText("LISTAR");
        ButtonListarSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonListarSaidaActionPerformed(evt);
            }
        });

        ButtonCancelar.setText("CANCELAR");
        ButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelarActionPerformed(evt);
            }
        });

        jLabel3.setText("Cod. Saida:");

        jLabel4.setText("Num. Lote:");

        jLabel5.setText("Produto:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(202, 202, 202)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(LabelNumLote, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(LabelCodSaida, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                    .addComponent(LabelProduto))
                .addContainerGap(221, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(240, 240, 240))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(354, 354, 354)
                                .addComponent(ButtonDeletarSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(ButtonListarSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(ButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 715, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(172, 172, 172)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBoxDataSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(ButtonPesqSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(21, 21, 21))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LabelCodSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(LabelNumLote, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(LabelProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxDataSaida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonPesqSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonDeletarSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonListarSaida, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonPesqSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonPesqSaidaActionPerformed
        TableSaida.getColumnModel().getColumn(0).setPreferredWidth(130);
        TableSaida.getColumnModel().getColumn(1).setPreferredWidth(300);
        TableSaida.getColumnModel().getColumn(2).setPreferredWidth(200);
        TableSaida.getColumnModel().getColumn(3).setPreferredWidth(200);
        TableSaida.getColumnModel().getColumn(4).setPreferredWidth(200);
        DefaultTableModel modelo = (DefaultTableModel) TableSaida.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Saida saida = new Saida();
        ArrayList<Saida> lista = new ArrayList<Saida>();
        try {
            saida.setDataSaida(txtBoxDataSaida.getText());
            lista = (ArrayList<Saida>) f.pesquisarSaida(saida);
            } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
           modelo.addRow(new Object[]{lista.get(i).getCodidentsaida(), lista.get(i).getCodProduto().getDescProduto(), 
                        lista.get(i).getNumLote(), lista.get(i).getQtdSaidaItens(), 
                        lista.get(i).getDataSaida()});
        }
        TableSaida.setModel(modelo);
    }//GEN-LAST:event_ButtonPesqSaidaActionPerformed

    private void ButtonListarSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonListarSaidaActionPerformed
        listarTabela();
        txtBoxDataSaida.setText("");
    }//GEN-LAST:event_ButtonListarSaidaActionPerformed

    private void TableSaidaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableSaidaMouseClicked
         int x = TableSaida.getSelectedRow();
        int y = TableSaida.getSelectedColumn();

        //Se o clique for duplo
        if (evt.getClickCount() == 1) {
            if (evt.getButton() == evt.BUTTON1) {//E o botão for o esquerdo

                LabelCodSaida.setText((String) TableSaida.getValueAt(x, 0));
                LabelNumLote.setText((String) TableSaida.getValueAt(x, 2));
                LabelProduto.setText((String) TableSaida.getValueAt(x, 1));
            }
        }
    }//GEN-LAST:event_TableSaidaMouseClicked

    private void ButtonDeletarSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDeletarSaidaActionPerformed
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Deletar?", "Deletar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            try {
                f = Fachada.getInstancia();
                Saida saida = new Saida();
                ItensProduto ip = new ItensProduto();
                saida.setCodidentsaida(LabelCodSaida.getText());
                ip.setNumLote(LabelNumLote.getText());
                saida.setNumLote(LabelNumLote.getText());
                Produto prod = new Produto();
            prod.setDescProduto(LabelProduto.getText());
        try {
            f.pesquisarProd(prod);
             } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (DaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
                f.deletarSaida(saida, ip, prod);
                LabelCodSaida.setText("");
                LabelNumLote.setText("");
                LabelProduto.setText("");
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConexaoException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (GeralException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonDeletarSaidaActionPerformed

    private void ButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelarActionPerformed
         telaPesquisarSaida tlpesqsaida = new telaPesquisarSaida();
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaPesquisarSaida.this.setVisible(false);
            tlpesqsaida.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaPesquisarSaida().setVisible(true);
            }
        });
    }
    
     public void listarTabela(){
        TableSaida.getColumnModel().getColumn(0).setPreferredWidth(130);
        TableSaida.getColumnModel().getColumn(1).setPreferredWidth(300);
        TableSaida.getColumnModel().getColumn(2).setPreferredWidth(200);
        TableSaida.getColumnModel().getColumn(3).setPreferredWidth(200);
        TableSaida.getColumnModel().getColumn(4).setPreferredWidth(200);
        DefaultTableModel modelo = (DefaultTableModel) TableSaida.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Saida saida = new Saida();
        ArrayList<Saida> lista = new ArrayList<Saida>();
        try {
            lista = (ArrayList<Saida>) f.listar(saida);
            } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
           modelo.addRow(new Object[]{lista.get(i).getCodidentsaida(), lista.get(i).getCodProduto().getDescProduto(), 
                        lista.get(i).getNumLote(), lista.get(i).getQtdSaidaItens(), 
                        lista.get(i).getDataSaida()});
        }
        TableSaida.setModel(modelo);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancelar;
    private javax.swing.JButton ButtonDeletarSaida;
    private javax.swing.JButton ButtonListarSaida;
    private javax.swing.JButton ButtonPesqSaida;
    private javax.swing.JLabel LabelCodSaida;
    private javax.swing.JLabel LabelNumLote;
    private javax.swing.JLabel LabelProduto;
    private javax.swing.JTable TableSaida;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JFormattedTextField txtBoxDataSaida;
    // End of variables declaration//GEN-END:variables
}
